package cn.iocoder.yudao.module.promotion.gift.service.app;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.service.impl.BaseService;
import cn.iocoder.yudao.module.api.promotion.gift.dto.GiftActivityVO;
import cn.iocoder.yudao.module.promotion.gift.convert.GiftActivityConvert;
import cn.iocoder.yudao.module.promotion.gift.dao.GiftActivityMapper;
import cn.iocoder.yudao.module.promotion.gift.pojo.dto.AppGiftActivityQueryDTO;
import cn.iocoder.yudao.module.promotion.gift.pojo.po.GiftActivity;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/01/13
 */
@Service
@Validated
public class AppGiftActivityServiceImpl extends BaseService<GiftActivity, GiftActivityVO, AppGiftActivityQueryDTO> implements AppGiftActivityService {
    @Resource
    private GiftActivityMapper giftActivityMapper;

    @Override
    protected GiftActivityMapper getInjectedMapper() {
        return giftActivityMapper;
    }

    @Override
    public GiftActivityVO convertToViewObject(@Nonnull GiftActivity entity) {
        GiftActivityVO vo = GiftActivityConvert.INSTANCE.convert(entity);
        if (vo.getStock() == null || vo.getStock() < 0) {
            vo.setStock(99999);
        }
        if (vo.getTakeLimitCount() == null || vo.getTakeLimitCount() < 0) {
            vo.setTakeLimitCount(99999);
        }
        vo.setTakeReceivedCount(0);
        return vo;
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<GiftActivity> buildQueryWrapper(AppGiftActivityQueryDTO param) {
        CoreQueryWrapper<GiftActivity> wrapper = super.buildQueryWrapper(param);
        wrapper.eqIfPresent("type", param.getType());
        wrapper.eqIfPresent("shop_id", param.getShopId());
        wrapper.eqIfPresent("spu_id", param.getSpuId());
        wrapper.eqIfPresent("coupon_template_id", param.getCouponTemplateId());
        wrapper.likeIfPresent("name", param.getName());
        LocalDateTime now = LocalDateTime.now();
        wrapper.geIfPresent("start_time", now);
        wrapper.ltIfPresent("end_time", now);
        return wrapper;
    }

    @Override
    public List<GiftActivityVO> getAvailableListByShopId(Long shopId, Integer limit) {
        List<GiftActivity> list = getInjectedMapper().selectAvailableListByShopId(shopId, limit);
        return GiftActivityConvert.INSTANCE.convertDTOList(list);
    }

}
